//
//  AboutViewController.m
//  iFlickr
//
//  Created by 张 霄鹤 on 09-5-17.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "AboutViewController.h"

#import "DataManager.h"


@implementation AboutViewController

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	// NSLocalizedString(@"KEY",@"Comment"); 这个宏 从默认名称的 .string 装载文字内容，如果是
	// 如果是自定义的名称，就需要调用 NSLocalizedStringFromTable(@"key",@"tablename",@"comment"); 来装载
	self.navigationItem.title = [NSString stringWithString:NSLocalizedStringFromTable(@"ABOUT_IFLICK",@"main",@"")];  // @"About iFlick";
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


#pragma mark -
#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	NSInteger ret = 0;
	switch (section) {
		case 0:
			ret = 3;
			break;
		case 1:
			// TODO Need Homepage
			//ret = 4;
			ret = 3;
			break;
		default:
			break;
	}
    return ret;
}

- (void)setInformationTitle:(NSInteger)row withCell:(UITableViewCell *)cell {
	NSString *version = nil;
	DataManager *dm;
	switch (row) {
		case 0:
			version  = [[[NSBundle mainBundle] infoDictionary] valueForKey:[NSString stringWithFormat:@"CFBundleVersion"]];
#ifndef __IPHONE_3_0
			cell.text = [NSString stringWithFormat:@"iFlick v%@",version];
#else
			cell.textLabel.text = [NSString stringWithFormat:@"iFlick v%@",version];
#endif
			break;
		case 1:
#ifndef __IPHONE_3_0
			cell.text = [NSString stringWithFormat:@"%@ %@",(NSLocalizedStringFromTable(@"AUTHOR",@"main",@"")),@"vhly[FR]"];
#else
			cell.textLabel.text = [NSString stringWithFormat:@"%@ %@",(NSLocalizedStringFromTable(@"AUTHOR",@"main",@"")),@"vhly[FR]"];
#endif
			break;
		
		case 2:
			dm = [DataManager sharedDataManager];
			if(dm.userName){
#ifndef __IPHONE_3_0
				cell.text = [NSString stringWithFormat:@"Flickr Account %@",dm.userName];
#else
				cell.textLabel.text = [NSString stringWithFormat:@"Flickr Account %@",dm.userName];
#endif
			}else{
#ifndef __IPHONE_3_0
				cell.text = [NSString stringWithString:(NSLocalizedStringFromTable(@"NO_LOGIN",@"main",@""))];
#else
				cell.textLabel.text = [NSString stringWithString:(NSLocalizedStringFromTable(@"NO_LOGIN",@"main",@""))];
#endif
			}
			break;


		default:
			// TODO 设置
#ifndef __IPHONE_3_0
			cell.text = @"Cell Need";
#else
			cell.textLabel.text = @"Cell Need";
#endif
			break;
	}
}


- (void)setWebsites:(NSInteger)row withCell:(UITableViewCell *)cell {
	switch (row) {
		//case 0:
//#ifndef __IPHONE_3_0
//			cell.text = [NSString stringWithString:(NSLocalizedStringFromTable(@"HOMEPAGE",@"main",@""))];
//#else
//			cell.textLabel.text = [NSString stringWithString:(NSLocalizedStringFromTable(@"HOMEPAGE",@"main",@""))];
//#endif
//			break;
		case 0:
#ifndef __IPHONE_3_0
			cell.text = @"flickr.com";
#else
			cell.textLabel.text = @"flickr.com";
#endif			
			break;
		case 1:
#ifndef __IPHONE_3_0
			cell.text = [NSString stringWithString:(NSLocalizedStringFromTable(@"TELL_FRIEND",@"main",@""))];
#else
			cell.textLabel.text = [NSString stringWithString:(NSLocalizedStringFromTable(@"TELL_FRIEND",@"main",@""))];
#endif			
			break;
		case 2:
#ifndef __IPHONE_3_0
			cell.text = [NSString stringWithString:(NSLocalizedStringFromTable(@"REPORT_BUG",@"main",@""))];
#else
			cell.textLabel.text = [NSString stringWithString:(NSLocalizedStringFromTable(@"REPORT_BUG",@"main",@""))];
#endif			
			break;
		default:
			// TODO 设置
#ifndef __IPHONE_3_0
			cell.text = @"Cell Need";
#else
			cell.textLabel.text = @"Cell Need";
#endif
			break;
	}
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
#ifndef __IPHONE_3_0
		cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
#else
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
#endif
    }
    
    // Set up the cell...
	
	NSString *fn = nil;
#ifndef __IPHONE_3_0
	fn = [cell.font.fontName copy];
#else
	fn = [cell.textLabel.font.fontName copy];
#endif
	
	UIFont *nf = [UIFont fontWithName:fn size:13.0f];
#ifndef __IPHONE_3_0
	cell.font = nf;
#else
	cell.textLabel.font = nf;
#endif
	[fn release];
	
	NSInteger section = indexPath.section;
	NSInteger row = indexPath.row;
	
	switch (section) {
		case 0:
			// 对于第一个section 都不能够选择
			cell.selectionStyle = UITableViewCellSelectionStyleNone;
			[self setInformationTitle:row withCell:cell];
			break;
		case 1:
			[self setWebsites:row withCell:cell];
			break;

		default:
			break;
	}
	
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
	NSString *ret = nil;
	switch (section) {
		case 0:
			ret = [NSString stringWithString:(NSLocalizedStringFromTable(@"SOFT_INFO",@"main",@""))];
			break;
		case 1:
			ret = [NSString stringWithString:(NSLocalizedStringFromTable(@"WEBSITE",@"main",@""))];
			break;

		default:
			break;
	}
	return ret;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
	// AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil];
	// [self.navigationController pushViewController:anotherViewController];
	// [anotherViewController release];
	//UIViewController *about = [[ViewControllerChooser sharedChooser] choose:VCC_ID_ABOUT];
	//	[self.navigationController pushViewController:about animated:YES];
	NSInteger section = indexPath.section;
	NSInteger row = indexPath.row;
	BOOL bOK;
	// Website 部分
	if(section == 1){
		switch (row) {
			//case 0:
//				// TODO 打开浏览器，访问 软件首页
//				break;
			case 0:
				// TODO 访问flickr.com
				bOK = [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://m.flickr.com"]];
				break;
			case 1:
				// TODO 针对直接设定 mailto:的，能够开启邮件客户端，在真机上可以开启
				bOK = [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto:"]];
				break;
				
			case 2:
				bOK = [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto:vhly@163.com?subject=iFlick%20Bug%20Report"]];
				if(!bOK){
#ifdef debug
					NSLog(@"Invoke Error with mailto");
#endif
				}
				break;

			default:
				break;
		}
	}
}


- (void)dealloc {
    [super dealloc];
}


@end
